package com.blb.thread;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.concurrent.*;

/**
 * 线程池案例
 */
public class ThreadPoolDemo {

    //不使用线程池，会造成大量线程资源的浪费
    public static void testNonThreadPool(){
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() +" 执行了");
            }).start();
        }
    }

    //长度不限的线程池
    public static void testCachedThreadPool(){
        //创建长度不限的线程池
        ExecutorService threadPool = Executors.newCachedThreadPool();
        for (int i = 0; i < 100; i++) {
            //使用线程池启动线程
            threadPool.execute(() -> {
                System.out.println(Thread.currentThread().getName() +" 执行了");
            });
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //关闭线程池
        threadPool.shutdown();
    }

    //长度固定的线程池
    public static void testFixedThreadPool(){
        //创建长度不限的线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 100; i++) {
            //使用线程池启动线程
            threadPool.execute(() -> {
                System.out.println(Thread.currentThread().getName() +" 执行了");
            });
        }
        //关闭线程池
        threadPool.shutdown();
    }

    //单一线程的线程池
    public static void testSingleThreadPool(){
        //创建长度单一的线程池
        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 100; i++) {
            //使用线程池启动线程
            threadPool.execute(() -> {
                System.out.println(Thread.currentThread().getName() +" 执行了");
            });
        }
        //关闭线程池
        threadPool.shutdown();
    }

    //可调度的线程池
    public static void testScheduledThreadPool(){
        //创建长度单一的线程池
        ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(10);
        //执行调度任务，参数1 任务 参数2 初始延迟 参数3 周期 参数4 时间单位
        threadPool.scheduleAtFixedRate(() -> {
            System.out.println("Hello!! " + LocalDateTime.now());
        },2,1, TimeUnit.SECONDS);
        //关闭线程池
//        threadPool.shutdown();
    }

    //自定义的线程池
    public static void testThreadPoolExecutor(){
        //获得内核数
        int cpuNum = Runtime.getRuntime().availableProcessors() * 2;
        //手动创建线程池
        ExecutorService threadPool = new ThreadPoolExecutor(cpuNum,cpuNum,0,TimeUnit.SECONDS,
                new LinkedBlockingQueue<>());
        for (int i = 0; i < 1000; i++) {
            //使用线程池启动线程
            threadPool.execute(() -> {
                System.out.println(Thread.currentThread().getName() +" 执行了");
            });
        }
        //关闭线程池
        threadPool.shutdown();
    }

    public static void main(String[] args) throws IOException {
//        testNonThreadPool();
//        testCachedThreadPool();
//        testFixedThreadPool();
//        testSingleThreadPool();
//        testScheduledThreadPool();
        testThreadPoolExecutor();
    }

}
